package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.BasicEnterpriseTypeEntity;
import com.iwomy.envpro.platform.entity.OperaParaEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.OperaParaService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 运行参数
 *
 * @author pig
 * @date 2023-08-25 10:46:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/operaPara")
@Tag(description = "operaPara", name = "运行参数管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OperaParaController {

	private final OperaParaService operaParaService;

	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param operaPara 运行参数
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_operaPara_view')")
	public R getOperaParaPage(@ParameterObject Page page, @ParameterObject OperaParaEntity operaPara) {
		LambdaQueryWrapper<OperaParaEntity> wrapper = Wrappers.lambdaQuery(operaPara);
		return R.ok(operaParaService.page(page, wrapper));
	}


	/**
	 * 通过id查询运行参数
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_operaPara_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(operaParaService.getById(id));
	}

	/**
	 * 新增运行参数
	 *
	 * @param operaPara 运行参数
	 * @return R
	 */
	@Operation(summary = "新增运行参数", description = "新增运行参数")
	@SysLog("新增运行参数")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_operaPara_add')")
	public R save(@RequestBody OperaParaEntity operaPara) {
		OperaParaEntity wrapperEntity = new OperaParaEntity();
		wrapperEntity.setConfigItem(operaPara.getConfigItem());
		LambdaQueryWrapper<OperaParaEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		OperaParaEntity one = operaParaService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			return  R.failed("运行参数已存在");
		}
		return R.ok(operaParaService.save(operaPara));
	}

	/**
	 * 修改运行参数
	 *
	 * @param operaPara 运行参数
	 * @return R
	 */
	@Operation(summary = "修改运行参数", description = "修改运行参数")
	@SysLog("修改运行参数")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_operaPara_edit')")
	public R updateById(@RequestBody OperaParaEntity operaPara) {
		OperaParaEntity wrapperEntity = new OperaParaEntity();
		wrapperEntity.setConfigItem(operaPara.getConfigItem());
		LambdaQueryWrapper<OperaParaEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		OperaParaEntity one = operaParaService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)&&!one.getId().equals(operaPara.getId())) {
			return  R.failed("运行参数已存在");
		}
		return R.ok(operaParaService.updateById(operaPara));
	}

	/**
	 * 通过id删除运行参数
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除运行参数", description = "通过id删除运行参数")
	@SysLog("通过id删除运行参数")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_operaPara_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(operaParaService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param operaPara 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_operaPara_export')")
	public List<OperaParaEntity> export(OperaParaEntity operaPara) {
		return operaParaService.list(Wrappers.query(operaPara));
	}
}